package services.persistance;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

public class BaseDAO {

	private static BaseDAO dao;
	private SessionFactory sessionFactory;
	/** ThreadLocal Session Map */
	public static final ThreadLocal map = new ThreadLocal();

	private BaseDAO() throws HibernateException {
		Configuration configuration = new Configuration();
		configuration = configuration.configure("persistence.cfg.xml");
		sessionFactory = configuration.buildSessionFactory();
	}

	public static synchronized BaseDAO getInstance() throws HibernateException {
		if (dao == null)
			dao = new BaseDAO();

		return dao;
	}

	public SessionFactory getSessionFactory() throws HibernateException {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory)
			throws HibernateException {
		closeSession();
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() throws HibernateException {
		Session s = (Session) map.get();
		// Open a new Session, if this Thread has none yet
		if (s == null) {
			s = sessionFactory.openSession();
			map.set(s);
		}
		return s;
	}

	public void closeSession() throws HibernateException {
		Session s = (Session) map.get();
		map.set(null);
		if (s != null) {
			s.close();
		}
	}

	public void ajouter(Object objet) throws HibernateException {
		Session session = getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(objet);
			transaction.commit();
			System.out.println("tajouta");
		} catch (Exception e) {
			e.printStackTrace();			
			System.out.println("ma tajouta");

			if (transaction != null)		
			{	
				System.out.println("trans khawia");

				transaction.rollback();
			}
		}
	}

	public void modifier(Object objet) throws HibernateException {
		Session session = getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.update(objet);

			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (transaction != null)
				transaction.rollback();
		}

	}

	public void supprimer(Object objet) throws HibernateException {
		Session session = getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(objet);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (transaction != null)
				transaction.rollback();
		}
	}

	public Object getObject(Class classe, Long id) throws HibernateException {
		Object objet;
		Session session = getSession();
		objet = session.load(classe, id);
		return objet;
	}

	public Object getObjectString(Class classe, String id)
			throws HibernateException {
		Object objet;
		Session session = getSession();
		objet = session.load(classe, id);
		return objet;
	}

	public Object getObjectInt(Class classe, int id) throws HibernateException {
		Object objet;
		Session session = getSession();
		objet = session.load(classe, id);
		return objet;
	}

	public List getListObject(String requete, List parametres) throws HibernateException{
		Session session = getSession();
		System.out.println("hahouwa ayjbed ");

		Query query = session.getNamedQuery(requete);
		System.out.println("hahowa jbed requete ");

		int taille = parametres.size();
		for (int i = 0; i < taille; i++) {
			query.setParameter(i, parametres.get(i));
		}

		List list = query.list();
		System.out.println("haliste t9adat ");

		return list;
	}

	public List getIdObjectInt(String requete, int parametres)
			throws HibernateException {
		Session session = getSession();
		Query query = session.getNamedQuery(requete);
		query.setParameter(0, parametres);

		List list = query.list();
		
		return list;
	}
	public List getIdEmplacementString(String requete, String parametres)
			throws HibernateException {
		Session session = getSession();
		Query query = session.getNamedQuery(requete);
		query.setParameter(0, parametres);

		List list = query.list();
		
		return list;
	}
	public List getListObjectByCriteria(Criteria criteria) {
		return criteria.list();
	}

}
